﻿using Admin.Core;
using Admin.Util;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Admin.Application.BaseManage
{
    /// <summary>
    /// 用户服务
    /// </summary>
    [ApiDescriptionSettings(ApiGroupConsts.USER_CENTER)]
    public class UserAppService : IUserAppService, IDynamicApiController, ITransient
    {
        /// <summary>
        /// 用户仓储
        /// </summary>
        private readonly ISqlSugarRepository<BaseUser> repository;
        private readonly ISqlSugarClient db;

        /// <summary>
        /// 用户管理类
        /// </summary>
        private readonly IOperator _operator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbRepository"></param>
        /// <param name="myOperator"></param>
        public UserAppService(ISqlSugarRepository<BaseUser> dbRepository
            , IOperator myOperator)
        {
            repository = dbRepository;
            db = repository.Context;
            _operator = myOperator;
        }

        public Task<PagedList<BaseUser>> GetListAsync([FromQuery, Required] PageInput input)
        {
            throw new NotImplementedException();
        }

        public Task<List<SecurityProfile>> GetSecuritiesAsync()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 查看当前用户拥有的菜单列表
        /// </summary>
        /// <returns></returns>
        [ApiDescriptionSettings(Name = "MenuList")]
        public async Task<List<UserMenu>> GetMenuListAsync()
        {
            var securitiesList = await _operator.GetSecuritiesAsync(_operator.UserId);
            var qList = securitiesList.Where(x => x.MenuType != MenuType.权限).ToList();

            var treeList = qList.Select(x => new UserMenu
            {
                Id = x.Id,
                Frame = x.IsFrame,
                Visible = x.IsVisible,
                Text = x.MenuName,
                ParentId = x.ParentId,
                Type = x.MenuType,
                Url = x.Component,
                Value = x.Id,
                Icon = x.MenuIcon,
                Sort = x.MenuSort
            }).ToList();

            return TreeHelper.BuildTree(treeList);
        }

        public BaseUser Profile()
        {
            throw new NotImplementedException();
        }

        public Task<BaseUser> ProfileAsync([Range(1, int.MaxValue, ErrorMessage = "请输入有效的用户 Id"), Required] int userId)
        {
            throw new NotImplementedException();
        }
    }
}
